﻿using Aliphant.Mvvm.XEvent.Events;
using Aliphant.Mvvm.XEvent.Events.Base.SubScription;
using Aliphant.Mvvm.XEvent.Ioc;

using CommonLibrary.Helper;
using CommonLibrary.UI;
using CuteWallpaperDemo.Managers;
using CuteWallpaperDemo.Models;
using CuteWallpaperDemo.UI.Windows;
using DataModel.Contants;
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Xml.Linq;

namespace CuteWallpaperDemo.Views
{
    /// <summary>
    /// NewMainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class NewMainWindow : CustomWindow
    {
        private ProjectAgent _projectAgent;
        private WindowEvent _windowEvent;

        SubScriptionToken? _subscriptionToken1;
        SubScriptionToken? _subscriptionToken2;

        public NewMainWindow()
        {
            InitializeComponent();

            this.Title = AppDefines.GetMainWindowTitle;
            _projectAgent = ProjectContainer.ProjectAgent;
            _windowEvent = _projectAgent.EventManager.GetEvent<WindowEvent>();

            RegisterRegions();
            RegistSubScriptionToken();
        }

        private void RegisterRegions()
        {
            XContainerLocator.Instance.RegisterRegion<ContentControl>(this.mainPlayer, "player");
            XContainerLocator.Instance.RegisterRegion<ContentControl>(this.mainPreview, "preivewer");
            XContainerLocator.Instance.RegisterRegion<ContentControl>(this.mainList, "pictures");
            XContainerLocator.Instance.RegisterRegion<ContentControl>(this.mainTools, "toolbar");
        }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            XContainerLocator.Instance.NavigateTo("player", "playControl");
            XContainerLocator.Instance.NavigateTo("preivewer", "picturePreview");
            XContainerLocator.Instance.NavigateTo("pictures", "mainPictureList");
            XContainerLocator.Instance.NavigateTo("toolbar", "toolsControl");
        }

        private void RegistSubScriptionToken()
        {
            _subscriptionToken1 = _windowEvent.Subscribe(OpenViewPictureWindow, (p) => p.WindowActionType == WindowActionType.Open && p.EventId == EventIds.OpenViewPictureWindow);
            _subscriptionToken2 = _windowEvent.Subscribe(OpenAddWindow, (p) => p.WindowActionType == WindowActionType.Open && p.EventId == EventIds.OpenAddWindow);
        }

        private void UnRegistSubScriptionToken()
        {
            _windowEvent.UnSubscribe(_subscriptionToken1);
            _windowEvent.UnSubscribe(_subscriptionToken2);
        }

        private void OpenViewPictureWindow(WindowEvent args)
        {
            WindowBase? window = new ViewPictureWindow();
            window.Parameter = args.Data;
            window.Owner = this;
            window.OnShowDialog();
        }

        private void OpenAddWindow(WindowEvent args)
        {
            WindowBase window = new SelectWindow();
            window.Parameter = args.Data;
            window.CallBack = args.CallBack;
            window.Owner = this;
            window.OnShowDialog();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (!AppConst.PupExitAlert)
            {
                CleanupAndClose(e);
                return;
            }

            if (ConfigManager.Instance.Settings.IsAlwaysAsk)
            {
                var res = MessageBox.Show("是否关闭？", "CuteWallpaper", MessageBoxButton.YesNo);
                if (res == MessageBoxResult.Yes)
                {
                    CleanupAndClose(e);
                    return;
                }
            }
            CancelClosing(e);
        }

        private void CancelClosing(CancelEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        private void CleanupAndClose(CancelEventArgs e)
        {
            NotifyHelper.Release();
            UnRegistSubScriptionToken();
            _windowEvent.Publish(new WindowEvent
            {
                Data = null,
                WindowActionType = WindowActionType.Close,
                EventId = EventIds.Release,
            });

            base.OnClosing(e);
        }

    }
}
